<?php
class retrieve_rss_admin extends retrieve_base{

	private $module_display_options 	= array();

		/**
	* This function is used to return the list of orders to be dealt with
	*/
	function getDigitalDesktop($parameters){
		$use=false;
		$out="";
		if($use==true){
		$data_files = $this->parent->site_directories["DATA_FILES_DIR"];
		$sql = "select * from rss_feed where rss_client=$this->client_identifier and rss_optout=0 and rss_digital_desktop=1 and rss_status=1 order by rss_label";
		$result  = $this->db->query($sql);
		$out ="";
		
    	while($r = $this->db->fetch_array($result)){
			$identifier					= $r["rss_identifier"];
			$filename 					= $r["rss_url"];
			$last_dowloaded				= $r["rss_downloaded"];
			$refresh					= $r["rss_frequency"];
			$bulletlist					= $r["rss_bulletlist"];
			$number_of_items			= $r["rss_number_of_items"];
			$rss_override_channel_title	= $r["rss_override_channel_title"];
			$rss_label					= $r["rss_label"];
			$download_now 	= 0;
			/*
				if not cached or inneed of refresh then download else use cache
			*/
			if (($last_dowloaded==0) || ($this->libertasGetTime()-$refresh>$last_dowloaded)){
				$download_now = 1;
			}
			if ($download_now==1){
				$file = @fopen ($filename, "r"); 
				$error=0;
				if (!$file) { 
				   $content ="<text><![CDATA[<p>Unable to find RSS Feed .\n<p>Please check the specified url <a href='$filename' target='_externalWindow'>$filename</a>.\n]]></text>";
				   $error=1;
				} else {
					$content ="";
					while (!feof ($file)) { 
					   $content .= fgets ($file, 1024); 
					} 
					$pos = strpos($content,"<rss");
					if ($pos===false){
						$pos = strpos($content,"<rdf");
					}
					if ($pos===false){
						//should not be here
						$content="";
					} else {
						$content = substr($content,$pos);
					}
					fclose($file); 
				}
				$pos = strpos($content,"<rss");
				if ($pos===false){
					$pos = strpos($content,"<rdf");
				}
				if ($pos===false){
					//should not be here
				} else {
					$content = substr($content,$pos);
				}
			$sql = "select * from rss_feed_fields where rff_feed = $identifier and rff_client = $this->client_identifier";
			$result  = $this->db->query($sql);
			$fields="<fields>
						<field name='bulletlist'><![CDATA[$rss_bulletlist]]></field>
						<field name='number_of_items'><![CDATA[$rss_number_of_items]]></field>
						<field name='override_channel_title'><![CDATA[$rss_override_channel_title]]></field>
						<field name='label'><![CDATA[$rss_label]]></field>
					";
            while($r = $this->db->fetch_array($result)){
               	$fields .= "<field name='show'><![CDATA[".$r["rff_field"]."]]></field>";
            }
			$fields .= "</fields>";
                $this->db->free_result($result);
				$filecontent= "<module name=\"RSS\" label=\"\" display=\"RSS\"><feed identifier='$identifier'>".$fields."</feed>".$content."</module>";
				$fp = fopen($data_files."/rss_feed_".$this->client_identifier."_".$identifier.".xml", 'w');
				fwrite($fp, $filecontent);
				fclose($fp);
				$um = umask(0);
				@chmod($data_files."/rss_feed_".$this->client_identifier."_".$identifier.".xml", LS__FILE_PERMISSION);
				umask($um);
				$out .=$filecontent;
				if ($error==1){
					$sql = "update rss_feed set rss_error_count=rss_error_count+1, rss_downloaded = '".time()."' where rss_client = $this->client_identifier and rss_identifier = $identifier ";
				} else {
					$sql = "update rss_feed set rss_error_count=0, rss_downloaded = '".time()."' where rss_client = $this->client_identifier and rss_identifier = $identifier ";
				}
	//			$sql = "update rss_feed set rss_downloaded = '".time()."' where rss_client = $this->client_identifier and rss_identifier = $identifier ";
				$this->db->query($sql);
			} else {
				if (file_exists($data_files."/rss_feed_".$this->client_identifier."_".$identifier.".xml")){
					$content_array = file($data_files."/rss_feed_".$this->client_identifier."_".$identifier.".xml");
					$out .= implode("", $content_array);
				}
			}
   	    }
       	$this->db->free_result($result);
		}
		return $out;
	}
	
	/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		$tables = array();
		/**
		* Table structure for table 'rss_feed'
		*/
		$fields = array(
			array("rss_identifier"				,"unsigned integer"			,"NOT NULL"	,"auto_increment","key"),
			array("rss_url"						,"text"						,"NOT NULL"	,"default ''"	),
			array("rss_label"					,"varchar(255)"				,"NOT NULL"	,"default '0'"	),
			array("rss_client"					,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("rss_downloaded"				,"unsigned integer"			,"NOT NULL"	,"default '0'"	),
			array("rss_frequency"				,"unsigned integer"			,"NOT NULL"	,"default '0'"	),
			array("rss_optout"					,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_created_by"				,"unsigned integer"			,"NOT NULL"	,"default '0'"	),
			array("rss_bulletlist"				,"unsigned small integer"	,"NOT NULL" ,"default '0'"	),
			array("rss_status"					,"unsigned small integer"	,"NOT NULL" ,"default '0'"	),
			array("rss_number_of_items"			,"unsigned small integer"	,"NOT NULL" ,"default '0'"	),
			array("rss_date_created"			,"datetime"					,"NOT NULL" ,"default ''"	),
			array("rss_set_inheritance"			,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_all_locations"			,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_digital_desktop"			,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_override_channel_title"	,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_error_count"				,"unsigned integer"			,"NOT NULL"	,"default '0'"	),
			array("rss_extract_set_inheritance"	,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_extract_all_locations"	,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_description"				,"text"						,"NOT NULL"	,"default ''"	),
			array("rss_external"				,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_type"					,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_extractable"				,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_new_window"				,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_needs_cached"			,"unsigned small integer"	,"NOT NULL"	,"default '0'"	),
			array("rss_last_download_attempt"	,"unsigned integer"			,"NOT NULL"	,"default '0'"	),
			array("rss_channel_image"			,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key")
		);
		
		$primary ="rss_identifier";
		$tables[count($tables)] = array("rss_feed", $fields, $primary);
		/**
		* Table structure for table 'rss_feed_fields'
		*/
		$fields = array(
			array("rff_identifier"				,"unsigned integer"			,"NOT NULL"	,"auto_increment","key"),
			array("rff_field"					,"varchar(255)"				,"NOT NULL"	,"default '0'"),
			array("rff_client"					,"unsigned integer"			,"NOT NULL"	,"default '0'","key"),
			array("rff_on"						,"unsigned integer"			,"NOT NULL"	,"default '1'"),
			array("rff_feed"					,"unsigned integer"			,"NOT NULL"	,"default '0'")
		);
		
		$primary ="rff_identifier";
		$tables[count($tables)] = array("rss_feed_fields", $fields, $primary);
		return $tables;
	}
}
?>